function [ res ] = tr2( m, n, W2 )
%TR2 Summary of this function goes here
%   Detailed explanation goes here
W=crop(W2);
W=W(:);

m2 = 0.5*diag(W(1:m*n - m), m);
m3 = 0.5*diag(W(1:m*n - m), -m);
m4 = 0.5*diag(W(m+1:m*n), m);
m5 = 0.5*diag(W(m+1:m*n), -m);
m6 = 0.5*diag(W(1:m*n-1), 1);
m7 = 0.5*diag(W(1:m*n-1), -1);
m8 = 0.5*diag(W(2:m*n), 1);
m9 = 0.5*diag(W(2:m*n), -1);

%m1 = 0.25 * diag(sum(m2+m3+m4+m5+m6+m7+m8+m9));
%m1 = diag(W,0);

M = zeros(m+2,n+2);
for i = 2:m+1
    for j = 2:n+1
        M(i,j) = W2(i+1,j) + W2(i-1,j) + W2(i,j-1) + W2(i,j+1); 
    end
end
M=crop(M);
m1 = 0.25 * diag(M(:));

res=m1+m2+m3+m4+m5+m6+m7+m8+m9;

end

